package com.lagou.service.impl;

import com.lagou.dao.ResourceCategoryMapper;
import com.lagou.dao.ResourceMapper;
import com.lagou.domain.Resource;
import com.lagou.domain.ResourceCategory;
import com.lagou.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ResourceCategoryServiceImpl implements ResourceCategoryService {

    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 查询资源分类信息
     *
     * @return
     */
    @Override
    public List<ResourceCategory> findAllResourceCategory() {

        return resourceCategoryMapper.findAllResourceCategory();
    }

    /**
     * 添加资源分类
     *
     * @param resourceCategory
     */
    @Override
    public void saveResourceCategory(ResourceCategory resourceCategory) {

        Date date = new Date();
        resourceCategory.setCreatedBy("system");
        resourceCategory.setCreatedTime(date);
        resourceCategory.setUpdatedBy("system");
        resourceCategory.setUpdatedTime(date);
        resourceCategoryMapper.saveResourceCategory(resourceCategory);
    }

    /**
     * 修改资源分类
     *
     * @param resourceCategory
     */
    @Override
    public void updateResourceCategory(ResourceCategory resourceCategory) {

        resourceCategory.setUpdatedTime(new Date());
        resourceCategory.setUpdatedBy("system");
        resourceCategoryMapper.updateResourceCategory(resourceCategory);
    }

    /**
     * 删除资源分类
     *
     * @param id 资源分类id
     */
    @Override
    public void deleteResourceCategory(Integer id) {

        resourceCategoryMapper.deleteResourceCategory(id);
    }

    /**
     * 获取当前角色拥有的 资源信息
     *
     * @param id 角色id
     * @return
     */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer id) {

        List<ResourceCategory> list = resourceCategoryMapper.findResourceCategoryByRoleId(id);
        for (ResourceCategory resourceCategory : list) {

            List<Resource> resourceList=resourceMapper.findResourceByCategoryId(resourceCategory.getId(),id);
            resourceCategory.setResourceList(resourceList);
        }

        return list;
    }
}
